#include <iostream>
using namespace std;

int fAnswers[500001];
int gAnswers[500001];

int g(int k);

int f(int k) {
	if (fAnswers[k] != 0) return fAnswers[k];
	
	
	if (k == 1) {
		fAnswers[k] = 1;
		return 1;
	}
	
	if (k % 2 == 0) {
		fAnswers[k] = 2 * f(k / 2 );
		return fAnswers[k];
	}
	
	fAnswers[k] = 2 * g (k / 2); 
	
	return fAnswers[k];
	
	
}

int g(int k) {
	if (gAnswers[k] != 0) return gAnswers[k];
	
	if (k == 1) {
		gAnswers[k] = 1;
		return 1;
	}
	
	if (k % 2 == 0) {
		gAnswers[k] = 2 * g(k / 2 ) - 1;
		return gAnswers[k];
	}
	gAnswers[k] = 2 * f((k / 2) + 1) - 1;
	return gAnswers[k];
}


int main() {
	
	int n;
	f(500000);
	while (true) {
		cin >> n;
		if (n == 0) break;
		cout << f(n) << "\n";
	}
}	
